﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RB.Domain.RelationalMapping;

namespace RB.Web.Security.Rdb
{
    public class AppMenuSet : TransactionSet<AppMenu>
    {
        internal IEnumerable<AppMenu> WhereByAppCode(string appCode, Guid userId)
        {
            var q = from t in this.AsQueryable()
                    join t1 in (new PermissionSet()).AsQueryable().DefaultIfEmpty() on t.PermCode equals t1.Code
                    where t.AppCode == appCode && !t.IsDisabled && (t.PermCode == null || t1.IsPublic)
                    select t;
            var q1 = from t in this.AsQueryable()
                     join t1 in (new PermissionSet()).AsQueryable() on t.PermCode equals t1.Code
                     join t2 in (new RolePermissionSet()).AsQueryable() on t1.Code equals t2.PermCode
                     join t3 in (new UserAccountRoleSet()).AsQueryable() on t2.RoleId equals t3.RoleId
                     where t.AppCode == appCode && !t.IsDisabled && t3.UserId == userId
                     select t;
            var list = q.ToList();
            var list1 = q1.ToList();
            return list.Union(list1).Distinct();
        }
    }
}
